/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		str.h
 * 位 置:
 *		inc/pub/str.h
 * 描 述:
 *		公用字符串类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-20  Simon   - 创建
 *
 **************************************************************/

#ifndef __PUB_STR_H__
#define __PUB_STR_H__

#include <string>
#include <map>

using namespace std;

class String : public string
{
public:
	String();
	String(const String& str);
	String(const char* str);
	String(const char* str, unsigned len);
	String(const string& str);
public:
	String& rtrim(char chr = 0x20);
	String& ltrim(char chr = 0x20);
	String& trim(char chr = 0x20);
	String& lpad(unsigned len, char chr = 0x20);
	String& rpad(unsigned len, char chr = 0x20);
	String& lpads(unsigned len);
	String& lpadz(unsigned len);
	String& rpads(unsigned len);
	String& ucase();
	String& lcase();
	String& tcopy(char* arr, unsigned len);
	String& tassign(char* arr, unsigned len);
	String& tassign(const char* arr);
	int toi();
	long tol();
	String& fromi(int i);
	String& froml(long l);
	bool match(const char* pattern);

	String& operator=(const char* src);
	String& operator=(const String& src);
	friend String operator+(const String& str1, const char* str2);
	friend String operator+(const char* str1, const String& str2);
	friend String operator+(const String& str1, const String& str2);
	friend bool operator==(const String& str1, const char* str2);
	friend bool operator==(const char* str1, const String& str2);
	friend bool operator==(const String& str1, const String& str2);
	friend bool operator!=(const String& str1, const char* str2);
	friend bool operator!=(const char* str1, const String& str2);
	friend bool operator!=(const String& str1, const String& str2);
};

template <typename T, bool ignoreCase = true, bool trim = true> class StringMap : public map<String, T>
{
public:
	typedef String key_type;
	typedef T mapped_type;
	typedef pair<String, T> value_type;
	typedef typename map<String, T>::size_type size_type;
	typedef typename map<String, T>::iterator iterator;
	typedef typename map<String, T>::const_iterator const_iterator;

	inline T& operator[](const key_type& key)
	{
		key_type _key = key;
		if (ignoreCase)
			_key.ucase();
		if (trim)
			_key.trim();

		return map<key_type, T>::operator[](_key);
	}

	inline pair<iterator,bool> insert(const value_type& value)
	{
		value_type _value = value;
		if (ignoreCase) _value.first.ucase();
		if (trim) _value.first.trim();
		return map<key_type, mapped_type>::insert(_value);
	}

	inline iterator insert(iterator position, const value_type& value)
	{
		value_type _value = value;
		if (ignoreCase) _value.first.ucase();
		if (trim) _value.first.trim();
		return map<key_type, mapped_type>::insert(_value);
	}

	inline size_type erase(const key_type& key)
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::erase(_key);
	}

	inline iterator find(const key_type& key)
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::find(_key);
	}

	inline const_iterator find(const key_type& key) const
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::find(_key);
	}

	inline size_type count(const key_type& key) const
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::count(_key);
	}

	inline iterator lower_bound(const key_type& key)
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::lower_bound(_key);
	}

	inline const_iterator lower_bound(const key_type& key) const
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::lower_bound(_key);
	}

	inline iterator upper_bound(const key_type& key)
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::upper_bound(_key);
	}

	inline const_iterator upper_bound(const key_type& key) const
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::upper_bound(_key);
	}

	inline pair<iterator,iterator> equal_range(const key_type& key)
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::equal_range(_key);
	}

	inline pair<const_iterator,const_iterator> equal_range(const key_type& key) const
	{
		key_type _key = key;
		if (ignoreCase) _key.ucase();
		if (trim) _key.trim();
		return map<key_type, mapped_type>::equal_range(_key);
	}
};

#endif
